package com.green.service.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.green.dao.manage.OrgDao;
import com.green.model.manage.Org;
import com.green.model.manage.Person;
import com.green.model.manage.enums.OrgEnum;
import com.green.service.common.ServiceContext;
import com.green.service.common.impl.BaseServiceImpl;
import com.green.service.manage.OrgService;

/**
 * 组织
 * 
 */
@Repository("orgService")
public class OrgServiceImpl extends BaseServiceImpl implements OrgService {
	@Autowired
	private OrgDao orgDao;

	@Override
	public int insertOrg(Org ct) {
		return orgDao.insertOrg(ct);
	}

	@Override
	public int updateOrg(Org ct) {
		return orgDao.updateOrg(ct);
	}

	@Override
	public int deleteOrg(Map<String, Object> param) {
		return orgDao.deleteOrg(param);
	}

	@Override
	public Org getOrgById(String id) {
		return orgDao.getOrgById(id);
	}

	@Override
	public List<Org> getOrgList(Map<String, Object> param) {
		return orgDao.getOrgList(param);
	}

	@Override
	public List<Org> getChildOrg(String parentId) {
		return orgDao.getChildOrg(parentId);
	}

	@Override
	public String getTreeData(String id,String number) {
		// 展示根节点
		String tree = "";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("orgType", OrgEnum.BLOC.name());
		param.put("number", number);
		List<Org> orgTop = this.getOrgList(param);
		if (orgTop.size() >= 0) {
			if (null == id || "".equals(id)) {
				tree = "[{id:'" + orgTop.get(0).getId() + "',name:'"
						+ orgTop.get(0).getName() + "',isParent:true}]";
			} else {
				// 展示根节点以外节点
				id = id.replace(" ", "+");
				List<Org> orgList = this.getChildOrg(id);
				tree = "[";
				for (int i = 0; i < orgList.size(); i++) {
					boolean flag = true;
					Org org = orgList.get(i);
					List<Org> list = this.getChildOrg(org.getId());
					// 展示到第七层（分店）即设为枝叶节点
					if (list == null || list.size() <= 0) {
						flag = false;
					}
					tree += "{id:'" + org.getId() + "',name:'" + org.getName()
							+ "',isParent:'" + flag + "'},";

				}
				if (tree.length() > 1) {
					tree = tree.substring(0, tree.length() - 1);
				}
				tree += "]";
			}
		}
		return tree;
	}

	@Override
	public String getJoinCityId() {
		Org org = getCurrentCityOrg();
		if(org!=null)
			return org.getId();
		return null;
	}
	public String getJoinCityLongNumber(){
		Org org = getCurrentCityOrg();
		if(org!=null)
			return org.getLongNumber();
		return null;
	}

	private Org getCurrentCityOrg() {
		Person person = ServiceContext.getLoginPerson();
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("longNumberLogin", person.getOrg().getLongNumber());
		param.put("orgType", OrgEnum.COMPANY);
		List<Org> orgList = getOrgList(param);
		if(orgList!=null && orgList.size()>0) 
			return orgList.get(0);
		return null;
	}
}
